
package com.niguang.work.show.common.config.wechatMini;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.api.impl.WxMaQrcodeServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * wechat mini configuration
 */
@Configuration
@ConditionalOnClass(WxMaService.class)
@EnableConfigurationProperties(WechatMiniProperties.class)
public class WechatMiniConfiguration {

    @Autowired
    private WechatMiniProperties properties;


    @Bean
    @ConditionalOnMissingBean
    public WxMaService WxMaService() {
        WxMaService wxMaService = new WxMaServiceImpl();
        WxMaInMemoryConfig configStorage = new WxMaInMemoryConfig();
        configStorage.setAppid(this.properties.getAppId());
        configStorage.setSecret(this.properties.getSecret());
        wxMaService.setWxMaConfig(configStorage);
        return wxMaService;
    }


    @Bean
    @ConditionalOnMissingBean
    public WxMaUserService wxMaUserService() {
        WxMaUserService wxMaUserService = new WxMaUserServiceImpl(this.WxMaService());
        return wxMaUserService;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMaQrcodeService wxMaQrcodeService() {
        WxMaQrcodeService wxMaQrcodeService = new WxMaQrcodeServiceImpl(this.WxMaService());
        return wxMaQrcodeService;
    }

}
